<?php
	/* **********************************************************/
	/* Arcans Project (2010)                                    */
	/* Jeu sous forme d'application web entièrement conçu       */
	/* et réalisé par Killian Le Maitre                         */
	/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
	/* **********************************************************/

	//******************************
	//classe de gestion des sessions
	//******************************
	class MgtSessionClass {
		//propriétés :
		private $onload;
		private $level;
		private $page;
		//méthodes :
		function __construct() {
			$page = htmlentities($_GET["p"]);
			switch($page) {
				//validation de l'inscription *********************************************************
				case "ainsv" :
					$code = htmlentities($_GET["i1"]);
					$mysqlainsv = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlainsv->resultFun("select count(*) as 'find' from unvalid_users where code='".$code."'"));
					if ($res["find"] == 1) {
						$mysqlinsv = new MysqlClass(2);
						$result = mysql_fetch_array($mysqlinsv->resultFun("select * from unvalid_users where code='".$code."'"));
						$pwd = md5(hash("sha256", $result["users"]).hash("sha256", $result["password"]));
						$mysqlinsv->resultFun('insert into users (users,password,email,level) values("'.$result["users"].'","'.$pwd.'","'.$result["email"].'",0)');
						$mysqlinsv->resultFun("delete from unvalid_users where code='".$code."'");
						$_SESSION["pseudo"] = $result["users"];
						$_SESSION["level"] = 0;
						$this->onload = 'onload="obj_load.sessOpen(1);"';
						$this->level = true;
					}
					else {
						$pseudo = htmlentities($_GET["i2"]);
						$res = mysql_fetch_array($mysqlainsv->resultFun("select count(*) as 'find' from users where users='".$pseudo."'"));
						if ($res["find"] == 1) {
							$this->onload = 'onload="obj_load.sessOpen(3);"';
							$this->level = false;
						}
						else {
							$this->onload = 'onload="obj_load.sessOpen(2);"';
							$this->level = false;
						}
					}
					$this->page = "i";
					break;
				//validation de la suppression du compte **********************************************
				case "asupv" :
					$code = htmlentities($_GET["i1"]);
					$mysqlsupv = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlsupv->resultFun("select count(*) as 'find' from unvalid_users where code='".$code."'"));
					if ($res['find'] == 1) {
						//suppression des données
						$mysqlsupv2 = new MysqlClass(2);
						$mysqlsupv2->resultFun("delete from unvalid_users where code='".$code."'");
						//finalisation
						$this->onload = 'onload="obj_load.sessOpen(6);"';
						$this->level = false;
					}
					else {
						$pseudo = htmlentities($_GET["i2"]);
						$res = mysql_fetch_array($mysqlsupv->resultFun("select count(*) as 'find' from users where users='".$pseudo."'"));
						if ($res["find"] == 1) {
							$this->onload = 'onload="obj_load.sessOpen(5);"';
							$this->level = false;
						}
						else {
							$this->onload = 'onload="obj_load.sessOpen(4);"';
							$this->level = false;
						}
					}
					$this->page = "i";
					break;
				//récupération du compte **************************************************************
				case "asupn" :
					$code = htmlentities($_GET["i1"]);
					$pseudo = htmlentities($_GET["i2"]);
					$mysqlsupn = new MysqlClass(0);
					$res = mysql_fetch_array($mysqlsupn->resultFun("select count(*) as 'find' from unvalid_users where code='".$code."'"));
					if ($res["find"] == 1) {
						//récupération des données
						$res = mysql_fetch_array($mysqlsupn->resultFun("select email from unvalid_users where code='".$code."'"));
						$pwd_tab_alp = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
						$pwd_tab = array_rand($pwd_tab_alp, 8);
						$pwd = "";
						foreach ($pwd_tab as $elt) $pwd .= $pwd_tab_alp[$elt];
						$mysqlsupn2 = new MysqlClass(2);
						$mysqlsupn2->resultFun("insert into users (users,password,email,level) values('".$pseudo."','".md5(hash("sha256", $pseudo) . hash("sha256", $pwd))."','".$res["email"]."',0)");
						$mysqlsupn2->resultFun("delete from unvalid_users where code='".$code."'");
						//envoi du mail
						mailSend($res["email"],"Arcans Project : Récupération de compte",array("validsupp",$pseudo,$pwd));
						//connexion
						$_SESSION["pseudo"] = $pseudo;
						$_SESSION["level"] = 0;
						//finalisation
						$this->onload = 'onload="obj_load.sessOpen(9);"';
						$this->level = true;
					}
					else {
						$res = mysql_fetch_array($mysqlsupn->resultFun("select count(*) as 'find' from users where users='".$pseudo."'"));
						if ($res["find"] == 1) {
							$this->onload = 'onload="obj_load.sessOpen(8);"';
							$this->level = false;
						}
						else {
							$this->onload = 'onload="obj_load.sessOpen(7);"';
							$this->level = false;
						}
					}
					$this->page = "i";
					break;
				//cas de la page à adresse vierge *****************************************************
				case "" :
					$this->page = "i";
					$this->onload = "";
					if (isset($_SESSION["pseudo"])) $this->level = true;
					else $this->level = false;
					break;
				//cas par défaut **********************************************************************
				default :
					$this->page = $page;
					$this->onload = "";
					if (isset($_SESSION["pseudo"])) $this->level = true;
					else $this->level = false;
					break;
			}
		}
		public function levelSession() { return $this->level; }
		public function onLoad() { return $this->onload; }
		public function actPage() { return $this->page; }
	}
?>